﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <vector>

using namespace std;

vector<Bullet> b1;
vector<Bullet> b2;
vector<date_map> maps;

int select_view=0;
bool is_up=1;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    resize(1100,980);

    t1=new tank(this);
    t2=new tank(this);
    key=new hand_keys(this);

    t1->player=1;
    t2->player=2;

    t1->x=150;
    t1->y=200;
    t1->angle=0;

    t2->x=950;
    t2->y=850;
    t2->angle=180;

    t1->count_xy(*t1);
    t2->count_xy(*t2);
    QTimer *time=new QTimer(this);
    time->start(20);
    connect(time,&QTimer::timeout,this,&MainWindow::Key_hand);

    bullet=new Bullets(this);
    mode=new Map(this);
    rule=new gamerule(this);
    set_ui=new showui(this);

    set=new setting(this);
    }

MainWindow::~MainWindow()
{
    delete ui;
    delete t1;
    delete t2;
    delete key;
    delete bullet;
    delete mode;
    delete rule;
}

void MainWindow::Key_hand(){
    if(select_view==0){
        t1->hide();
        t2->hide();
        mode->hide();
        bullet->hide();
        rule->hide();
        set_ui->show();
        set_ui->update();
        set->hide();
    }
    else if(select_view==1){
        set_ui->hide();

        t1->show();
        t2->show();
        mode->show();
        bullet->show();
        rule->show();
        set_ui->hide();

        key->recive_key(*t1,*t2);
        t1->update();
        t2->update();
        mode->update();
        bullet->Bullet_to_wall(b1);
        bullet->Bullet_to_wall(b2);
        bullet->Bullet_move();
        bullet->Bullet_to_tank(b1,*t1);
        bullet->Bullet_to_tank(b1,*t2);
        bullet->Bullet_to_tank(b2,*t1);
        bullet->Bullet_to_tank(b2,*t2);
        bullet->Bullet_fire(*t1,*t2);
        bullet->update();
        rule->isWin(*t1,*t2);
        rule->update();

        set->hide();
    }
    else if(select_view==3){
        set_ui->hide();
        mode->hide();
        t1->hide();
        t2->hide();
        bullet->hide();

        set->show();
        set->update();
    }
    else if(select_view==2){
        t1->show();
        t2->show();
        mode->show();
        bullet->show();
        rule->show();

        set_ui->hide();
        bullet->update();
        bullet->Bullet_move();
        t1->update();
        t2->update();
        bullet->Bullet_fire(*t1,*t2);
        bullet->Bullet_to_tank(b1,*t1);
        bullet->Bullet_to_tank(b1,*t2);
        bullet->Bullet_to_tank(b2,*t1);
        bullet->Bullet_to_tank(b2,*t2);
        bullet->Bullet_to_wall(b1);
        bullet->Bullet_to_wall(b2);
        t2->machine(*t2);
        rule->isWin(*t1,*t2);
        rule->update();
        if(t2->isLive==1){
            if(clock()-t2->shoot_time>=300){
                if(b2.size()<30){
                    Bullet temp={
                        t2->x,
                        t2->y,
                        t2->angle+90,
                        10000,
                    };
                    b2.push_back(temp);
                    t2->shoot_time=clock();
                }
            }
        }
        key->recive_key(*t1,*t2);
        set->hide();
    }
}
